<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-col :span="4">
        <el-tree
          node-key="id"
          :default-expanded-keys="defaultKeys"
          :data="treeData"
          :props="defaultProps"
          @node-click="handleNodeClick"
        ></el-tree>
      </el-col>
      <el-col :span="20">
        <el-form
          v-if="isFirst"
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
        >
          <el-form-item label="数据集名称" prop="setName">
            <el-input
              v-model="queryParams.setName"
              placeholder="请输入"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
        <el-form
          v-else
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
        >
          <el-form-item label="起始时间" prop="startTime">
            <el-date-picker
              v-model="startTime"
              type="datetime"
              format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期时间"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="截止时间" prop="endTime">
            <el-date-picker
              v-model="endTime"
              type="datetime"
              format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期时间"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >查询</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
            <el-button icon="el-icon-download" size="mini" @click="exportHandle"
              >下载</el-button
            >
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              v-if="isFirst"
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              >新增</el-button
            >
          </el-col>
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="dataList" v-if="isFirst">
          <el-table-column
            prop="id"
            label="数据集id"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="setType"
            label="数据集类型"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="setName"
            label="数据集名称"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="setDesc"
            label="数据集描述"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="setCount"
            label="数据量"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="modelName"
            label="应用模型"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="versionNo"
            label="应用模型版本"
            align="center"
          ></el-table-column>
          <el-table-column label="下载" align="center" prop="setFiles">
            <template slot-scope="scope">
              <div v-for="(item, index) in scope.row.files" :key="index">
                <a
                  :href="`${downloadUrl}${item.filePath}`"
                  :download="item.originalFilename"
                  >{{ item.originalFilename }}</a
                >
              </div>
            </template>
          </el-table-column>
          <el-table-column
            v-if="isFirst"
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                >编辑</el-button
              >
              <el-button
                v-if="scope.row.parentId != 0"
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-table v-loading="loading" :data="dataList" v-else>
          <el-table-column
            v-for="(item, index) in tableHeaders"
            :key="index"
            :prop="item.key"
            :label="item.label"
            show-overflow-tooltip
            align="center"
          ></el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改对话框 -->
    <Edit
      v-if="dialogVisible"
      :dialogTitle="dialogTitle"
      :dialogOpen="dialogOpen"
      :dialogData="dialogData"
      @closeDialog="closeDialog"
    />
  </div>
</template>

<script>
import {
  listAiSets,
  getAiSetList,
  searchRadarList,
  setDownload,
  deleteSetByIds, searchOriginalList,
} from "@/api/aifactory";
import Edit from "./components/edit.vue";
import moment from "moment";
export default {
  name: "Aiset",
  components: { Edit },
  data() {
    return {
      treeData: [],
      defaultKeys: [1],
      pid: 1,
      currSetId: 0,
      downloadUrl: process.env.VUE_APP_BASE_API + "/file/download?fileName=/",
      isFirst: true,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      dataList: [],
      // 重新渲染表格状态
      refreshTable: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        current: 1,
        pageSize: 10,
      },
      startTime: moment().subtract(1, "days").format("YYYY-MM-DD HH:mm"),
      endTime: moment(new Date()).format("YYYY-MM-DD HH:mm"),
      defaultProps: {
        children: "children",
        label: "label",
        id: "id",
      },
      tableHeaders: [],
      //弹窗参数
      dialogVisible: false,
      dialogTitle: "",
      dialogOpen: false,
      dialogData: {},
    };
  },
  mounted() {
    this.setTrees();
    this.getList();
  },
  methods: {
    setTrees() {
      listAiSets().then((res) => {
        if (res.code === 200) {
          const opts = [];
          res.data.forEach((item) => {
            let opt = {};
            opt.id = item.id;
            opt.label = item.label;
            opt.children = item.children;
            opts.push(opt);
          });
          console.log(270, opts);
          this.treeData = opts;
        }
        this.defaultKeys = [this.treeData[0].id];
        console.log(261, this.treeData);
      });
    },
    handleNodeClick(data) {
      console.log("handleNodeClick", data);
      if (data.hasOwnProperty("children")) {
        this.isFirst = true;
        this.pid = data.id;
        console.log("isFirst1", this.isFirst);
        this.getList();
      } else {
        this.isFirst = false;
        this.currSetId = data.id
        this.dataList = null;
        this.total = 0;
        this.queryParams.currSetId = data.id;
        switch (data.id) {
          case 6:
            this.tableHeaders = [
              { label: "通道", key: "sourceId" },
              { label: "探测时间", key: "sendTime" },
              { label: "跟踪标志", key: "targetStatus" },
              { label: "d距离", key: "ddistance" },
              { label: "p距离", key: "pdistance" },
              { label: "方位", key: "direction" },
              { label: "多普勒值", key: "ddoppler" },
              { label: "径向速度", key: "vr" },
              { label: "速度", key: "speed" },
              { label: "经度", key: "longitude" },
              { label: "纬度", key: "latitude" },
              { label: "幅度", key: "am" },
              { label: "模式", key: "mode" },
              { label: "pd转换系数", key: "pd" },
              { label: "帧号", key: "mgs" },
              { label: "波位号", key: "zoneNo" },
              { label: "使用频率", key: "freq" },
              { label: "信噪比", key: "snr" },
            ];
            // delete this.queryParams.pid
            this.queryParams.startTime = this.startTime
            this.queryParams.endTime = this.endTime
            this.queryParams.currSetId = data.id
            searchRadarList(this.queryParams).then(res => {
              console.log('searchRadarList', res)
            })
            break;
          case 7:
            this.tableHeaders = [
              { label: "通道", key: "sourceId" },
              { label: "发送时间", key: "sendTime" },
              { label: "点数", key: "pointNum" },
            ];
            break;
          case 8:
            this.tableHeaders = [
              { label: "发送时间", key: "sendTime" },
              { label: "波束编号", key: "beamNumber" },
              { label: "规划类型", key: "modeType" },
              { label: "扫描波位号", key: "zoneNo" },
              { label: "帧序号", key: "mgs" },
              { label: "工作频率", key: "frequency" },
              { label: "信号带宽", key: "signalBandwidth" },
              { label: "信号时宽", key: "signalTimewidth" },
              { label: "波束数目编码", key: "beamNo" },
              { label: "积累点数", key: "pointNum" },
            ];
            break;
          case 9:
            this.tableHeaders = [
              { label: "雷达编号", key: "radarNumber" },
              { label: "字节模式", key: "byteMode" },
              { label: "发送时间", key: "receiveTime" },
              { label: "波束号", key: "beamNumber" },
              { label: "波位号", key: "beamPositionNumber" },
              { label: "信号形式", key: "signalForm" },
              { label: "信号带宽", key: "signalBandwidth" },
              { label: "脉冲重复周期", key: "repetitionPeriod" },
              { label: "频率个数", key: "frequency" },
              { label: "距离单元数", key: "distanceNo" },
              { label: "距离步进", key: "distanceStep" },
              { label: "距离", key: "distance" },
              { label: "回波幅度", key: "amplitude" },
            ];
            break;
          case 10:
            this.tableHeaders = [
              { label: "发送时间", key: "sendTime" },
              { label: "点数", key: "pointNum" },
            ];
            break;
        }
      }
    },
    /** 查询部门列表 */
    getList() {
      this.loading = true;
      this.queryParams.pid = this.pid;
      if (this.queryParams.pid != 1) {
        getAiSetList(this.queryParams).then((response) => {
          response.rows.forEach((item) => {
            let files = [];
            if (JSON.parse(item?.setFiles).length > 0) {
              JSON.parse(item.setFiles).forEach((item1) => {
                item1.name = item1.newFileName;
                item1.url = item1.filePath;
                // item1.url = window.location.origin + window.location.pathname + '/'+item1.filePath
                files.push(item1);
              });
              item.files = files;
            } else {
              item.files = [];
            }
          });

          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      }
      else {
        if (this.queryParams.currSetId) {
          searchOriginalList(this.queryParams).then((response) => {
            this.dataList = response.rows;
            this.total = response.total;
          });
        }
        this.loading = false;
      }
    },
    // 下载
    exportHandle() {
      let data = {
        currSetId: this.currSetId,
        startTime: this.startTime,
        endTime: this.endTime
      }
      console.log('download', data)
      setDownload(data).then((res) => {});
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //关闭库相关
    closeDialog(data) {
      console.log(397, data)
      this.dialogVisible = false;
      this.dialogOpen = false;
      this.dialogData = {}
      // this.pid = data
      // this.defaultKeys = [data]
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.dialogOpen = true;
      this.dialogTitle = "新增";
      this.dialogVisible = true;
      this.dialogData = {pid: this.pid};
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(218, row);
      this.dialogOpen = true;
      this.dialogTitle = "编辑";
      this.dialogVisible = true;
      this.dialogData = row;
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm("是否删除该数据项？")
        .then(function () {
          return deleteSetByIds([row.id]);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
